package com.sunshine.controller.mybatisCURD;

import com.sunshine.bean.UserInfo;
import com.sunshine.util.GetSqlSession;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by WeiHong on 2016/12/2.
 */
@WebServlet(name = "InsertUserInfoServlet", value = "/insertUserInfo")
public class InsertUserInfoServlet extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(InsertUserInfoServlet.class);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserInfo info = new UserInfo();
        info.setPassword("1111");
        info.setUsername("Petter23....");
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        try {

            sqlSession.insert("mybatis.userInfo.insertUserInfo", info);
        } catch (Exception e) {
            GetSqlSession.rollback();
            LOGGER.error("insertUserInfo error", e);
        } finally {
            GetSqlSession.commit();
        }

    }
}
